﻿using Amazon;
using Amazon.S3;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Triples.Model {
    class LocalScoresTable {


        public ObservableCollection<LocalHighscoreItem> Scores {
            get;
            private set;
        }


        public LocalScoresTable() {
            IEnumerator<LocalHighscoreItem> enumerator;
            LocalHighscoreItem score;

            /* Create the database if it does not exist. */
            using (LocalDataContext db = new LocalDataContext(LocalDataContext.DBConnectionString)) {

                /* Create the local database. */
                if (db.DatabaseExists() == false) db.CreateDatabase();

                /* Specify the query for all to-do items in the database. */
                var scores = (from LocalHighscoreItem s in db.Scores select s).OrderByDescending (item => item.Score);

                /* Query the database and load all to-do items. */
                Scores = new ObservableCollection<LocalHighscoreItem>();
                enumerator = scores.GetEnumerator();

                for (int i = 1; i <= 10; i++) {
                    if (!enumerator.MoveNext()) break;

                    score = enumerator.Current;
                    score.Rank = i;
                    Scores.Add(score);
                }
            }
        }


        internal void AddScore(string name, int score) {
            
            /* Create the database if it does not exist. */
            using (LocalDataContext db = new LocalDataContext(LocalDataContext.DBConnectionString)) {
                db.Scores.InsertOnSubmit(new LocalHighscoreItem { Name = name, Score = score });
                db.SubmitChanges();
            }
        }
    }
}
